﻿using MobilesStore.Domain.Abstract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MobilesStore.Domain.Entities;
namespace MobilesStore.Domain.Concrete
{
    public class EFProductRepository : IProductsRepository
    {
        private EFDBContext context = new EFDBContext();

        public IQueryable<Product> Products
        {
            get { return context.Products; }
        }


        public void SaveProduct(Product product)
        {
            if (product.ProductID == 0)
            {
                context.Products.Add(product);
            }
            else {
                Product dbEntry = context.Products.Find(product.ProductID);
                if (dbEntry != null)
                {
                    dbEntry.ProductName = product.ProductName;
                    dbEntry.ProductDescription = product.ProductDescription;
                    dbEntry.ProductPrice = product.ProductPrice;
                    dbEntry.ProductCategory = product.ProductCategory;
                    dbEntry.ImageData = product.ImageData;
                    dbEntry.ImageMimeType = product.ImageMimeType;
                    dbEntry.Quantity = product.Quantity;
                }
            }

            context.SaveChanges();
        }


        public Product DeleteProduct(int productID)
        {
            Product dbEntry = context.Products.Find(productID);

            if (dbEntry != null)
            {
                context.Products.Remove(dbEntry);
                context.SaveChanges();
            }
            return dbEntry;
        }
    }
}
